define(['zepto', 'JST', 'ctx', 'utils', 'wxConfig', 'wxShare', 'iscroll', 'lazyLoad', 'suiExtend', 'sui'], function($, JST, ctx, utils, wxConfig, wxShare, IScroll, lazyLoad) {
	return {

		init: function() {
			var that = this

			this.model={
				'page': 1,
			};

			this.onMenuClick();  //right menus

			this.compileArticlePage();	// compile article page

			this.productScroll();

			lazyLoad.piclazyload();
      
			wxConfig.config(
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone'
      );
			wxShare.openShare("活动新闻", "人马君最新活动最新动态尽在掌握…");

			this.model.pageScroll = new IScroll('.content', {
				click: that.iScrollClick()
			});

	        this.model.pageScroll.on('scrollEnd', function(e){
	        	
	        	if (this.maxScrollY < 0 && this.startY + this.distY <= this.maxScrollY - 50) {

	      			if(!that.model.endLoad) that.pageLoad()
	    	    }
	        })
		},

		iScrollClick :function(){

			if (/iPhone|iPad|iPod|Macintosh/i.test(navigator.userAgent)) return false;
			if (/Chrome/i.test(navigator.userAgent)) return (/Android/i.test(navigator.userAgent));
			if (/Silk/i.test(navigator.userAgent)) return false;
			if (/Android/i.test(navigator.userAgent)) {
	  		var s=navigator.userAgent.substr(navigator.userAgent.indexOf('Android')+8,3);
	  		return parseFloat(s[0]+s[3]) < 44 ? false : true
    			}
		},

		onMenuClick: function(){

			$(document).on("click", ".header-menu", function() {
			  $.openPanel("#panel-right-menu");
			});
		},

		getUrlVars: function(){
		    var vars = [], hash;

		    if(window.location.href.indexOf('?') == -1) {
		    	return null;
		    }else {
		    	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			    for(var i = 0; i < hashes.length; i++)
			    {
			    	hash = hashes[i].split('=');
			    
			    	vars[hash[0]] = hash[1];
			    }
			    return vars;
		    }
		    
	  	},

	  	getNewsActivity: function() {
	  		var that = this;

	  		var defer = $.Deferred(),
	  		 	url   = ctx.host + 'activities?per_page=2'
	  		//show request indicator
	  		$.showIndicator();

	  		//request for rounds data
	  		$.ajax({
	  			url: 	url,
	  		   type:   'GET',
	  		  cache:    false
	  		})
	  		.done(function(response) {
	  			$.hideIndicator();
	  			utils.processResult(response, function(result){
	  				defer.resolve(result.data)
	  			},function(result){
	  				$.toast(result.message || '获取文章模块失败')
	  			})
	  		})
	  		
	  		return defer.promise()

	  	},

	  	getNewArticle: function() {

			var that = this;
		
			var defer = $.Deferred(),
	  			url   = ctx.host + 'news?page=' + that.model.page

			//show request indicator
			$.showIndicator();

			//request  for rounds data
			$.ajax({
				url: 	url,
				type:  'GET',
				cache:  false
			})
			.done(function(response) {
				$.hideIndicator();
				utils.processResult(response, function(result){
					defer.resolve(result.data)
				},function(result){
					$.toast(result.message || '获取文章模块失败')
				})
			})
			
			return defer.promise()

		},
		compileArticlePage: function(page) {
			var that = this,
     			deferNewsArticle = that.getNewsActivity();
          		deferArticle = that.getNewArticle();

          	$.when(deferNewsArticle).done(function(newsActivityData){

      			//add data to article model & page template
      			$article =  JST.NewsActivityTpl({
      				'activitypages'  : newsActivityData.activities,
      				'domain'	: ctx.domain,
      				'imgsurl'	: ctx.cdn
    
      			})

      			$("#news-activity").empty().append($article)
      			
      		});

       		$.when(deferArticle).done(function(newsData){

      			//add data to article model & page template
      			$article =  JST.NewsArticleTpl({
      				'news'  : newsData.news,
      				'domain'	: ctx.domain,
      				'imgsurl'	: ctx.cdn
    
      			})

      			$("#news-article").empty().append($article)
      			that.model.pageScroll.refresh()
      			that.model.page = that.model.page + 1;
      		});
		},

		productScroll: function() {

			new IScroll("#newsscroll", {
		        scrollX: true,
		        scrollY: false,
		        eventPassthrough: true
		    }).on('scrollEnd', function(e) {
		        if (this.maxScrollX < 0 && this.startX + this.distX <= this.maxScrollX - 150) {
		            var more = this.scroller.querySelector('.card-more a')

	      	      	if (more) {
	      	      		window.location.href = $(more).attr("href")
      	      		}
	    	    }
	        })
		},

		pageLoad: function(){
			var that = this,
				deferArticle = that.getNewArticle();

			$.when(deferArticle).done(function(newsData){

      			//add data to article model & page template
      			$article =  JST.NewsArticleTpl({
      				'news'  : newsData.news,
      				'domain'	: ctx.domain,
      				'imgsurl'	: ctx.cdn
    
      			})

      			if(!newsData.page.nextPage) {
      				that.model.endLoad = true;
      				$(".pull-color").html("已无更多文章")
      			}

      			$("#news-article").append($article)
      			that.model.pageScroll.refresh()
      			that.model.page = that.model.page + 1;
      		});


		}
	
	}
})